import { Component, OnInit } from '@angular/core';
import { PaginationModel } from 'app/widgets/pagination';
import { environment } from '../../../../../environments/environment';
import { FundPageService } from "./fund-page.service";
import * as flatpickr from 'flatpickr';
import { zh } from "flatpickr/dist/l10n/zh.js";
import * as _ from 'lodash';
import { DataService } from '../../../../services/data.service';

@Component({
  selector: 'app-fund-page',
  templateUrl: './fund-page.component.html',
  styleUrls: ['./fund-page.component.scss'],
  providers: [FundPageService]
})
export class FundPageComponent implements OnInit {
  // lodash
  public _: any = _;
  // 菜单数据
  public menuData: any;
  // 菜单权限set
  public menuResourceSet: any = [];
  // 分页列表post
  public getBody = {
    pageIndex: 1,
    pageSize: 10,
    sBegin: '',
    sEnd: '',
    nFundNoList: '',
    dateList: '',
    sFundCode: '',
    sFundName: '',
    sFundType: '',
    sBiztype: '',
    nCashType: '',
    sBiztypeList: '',
    nCashTypeList: ''
  };
  // 分页配置
  public listPageInfo: PaginationModel = {
    currentPageNum: 1,
    pageSize: 10,
    totalPages: 1,
    total: 0,
    pagesShow: 5,
    startRow: 0,
    endRow: 0,
    pageList: [5, 10, 25, 50, 100]
  };
  // 分页列表数据
  public dataList: any;
  // 列表全选
  public isCheckedAll: any = false;
  // 字典值
  public dictionaryList: any = {
    FUND_TYPE: [],
    BIZ_TYPE: [],
    CASHTYPE: []
  };

  constructor(
    public fundPageService: FundPageService,
    public dataService: DataService
  ) { }

  ngOnInit() {
    // 初始化日期控件
    flatpickr.localize(zh);
    // 字典值
    this.dictionaryList = JSON.parse(localStorage.getItem("dictionaryList"));
    // 权限处理
    let that = this;
    this.dataService.initTree.subscribe((data) => {
      if (data) {
        let SessionMenuData = sessionStorage.getItem('menu');
        this.menuData = JSON.parse(SessionMenuData)
        this.menuResourceSet = [];
        _.forEach(this.menuData, (item, index) => {
          if (item.sName == '综合查询') {
            _.forEach(item.children, (it, idx) => {
              if (it.sName == '资金流水查询') {
                that.menuResourceSet = it.resourceSet
                sessionStorage.setItem('btn-productInfo', this.menuResourceSet);
              }
            })
          }
        })
      }
    })
    let SessionMenuData = sessionStorage.getItem('menu');
    this.menuData = JSON.parse(SessionMenuData)
    this.menuResourceSet = [];
    _.forEach(this.menuData, (item, index) => {
      if (item.sName == '综合查询') {
        _.forEach(item.children, (it, idx) => {
          if (it.sName == '资金流水查询') {
            that.menuResourceSet = it.resourceSet
            sessionStorage.setItem('btn-productInfo', this.menuResourceSet);
          }
        })
      }
    })
    this.list();
  }
  // 分页列表
  list(pageNum?, pageSize?) {
    let that = this;
    this.isCheckedAll = false;
    this.getBody.pageIndex = pageNum ? pageNum : this.listPageInfo.currentPageNum;
    this.getBody.pageSize = pageSize ? pageSize : this.listPageInfo.pageSize;
    this.getBody.sFundCode = _.trim(this.getBody.sFundCode);
    this.getBody.sFundName = _.trim(this.getBody.sFundName);
    this.fundPageService.list(this.getBody).subscribe(data => {
      if (data) {
        that.dataList = data.list;
        that.listPageInfo.currentPageNum = data.pageNum;
        that.listPageInfo.totalPages = data.pages;
        that.listPageInfo.total = data.total;
        that.listPageInfo.startRow = data.startRow;
        that.listPageInfo.endRow = data.endRow;
      }
    });
  }
  //个数分页
  listPageSizeChange(pageSize: number) {
    if (pageSize !== this.listPageInfo.pageSize) {
      this.listPageInfo.pageSize = pageSize;
      this.list();
    }
  }
  // 条数分页
  listPageNavigation(currentPageNum: number) {
    this.listPageInfo.currentPageNum = currentPageNum;
    this.list(this.listPageInfo.currentPageNum, this.listPageInfo.pageSize);
  }
  // 列表全选
  checkedAll() {
    if (this.isCheckedAll) {
      _.forEach(this.dataList, item => {
        item.isChecked = true;
      });
    } else {
      _.forEach(this.dataList, item => {
        item.isChecked = false;
      });
    }
  }
  // 列表是否全选
  ctrlCheckAll() {
    let that = this;
    let checkList = [];
    _.forEach(this.dataList, item => {
      if (item.isChecked == true) {
        checkList.push(item);
      }
    })
    if (checkList.length == this.dataList.length) {
      this.isCheckedAll = true;
    } else {
      this.isCheckedAll = false;
    }
  }
  // 导出
  exportExcel() {
    let that = this;
    let fundNoList = [];
    let dateList = [];
    let sBiztypeList = [];
    let nCashTypeList = [];
    _.forEach(that.dataList, item => {
      if (item.isChecked) {
        fundNoList.push(item.nFundNo);
        dateList.push(item.tDate);
        sBiztypeList.push(item.sBiztype);
        nCashTypeList.push(item.nCashType)
      }
    })
    // let converItem: any = checkedItem.join(',');
    let getBody = {
      "nFundNoList": fundNoList,
      "dateList": dateList,
      "sBegin": this.getBody.sBegin,
      "sEnd": this.getBody.sEnd,
      "sFundCode": this.getBody.sFundCode,
      "sFundName": this.getBody.sFundName,
      "sFundType": this.getBody.sFundType,
      "sBiztype": this.getBody.sBiztype,
      "nCashType": this.getBody.nCashType,
      "sBiztypeList": sBiztypeList,
      "nCashTypeList": nCashTypeList
    }
    const iframe = window['$']("<iframe id='downloadiframe'>");
    iframe.attr('style', 'display:none');
    //  转码，兼容ie
    getBody.sFundCode = encodeURI(getBody.sFundCode);
    getBody.sFundName = encodeURI(getBody.sFundName);
    iframe.attr('src', environment.server + '/pms/v1/productquery/export/exportAccountCashflow?token=' + sessionStorage.getItem('token') + '&nFundNoList=' + getBody.nFundNoList + '&dateList=' + getBody.dateList + '&sBegin=' + getBody.sBegin + '&sEnd=' + getBody.sEnd + '&sFundCode=' + getBody.sFundCode + '&sFundName=' + getBody.sFundName + '&sFundType=' + getBody.sFundType + '&sBiztype=' + getBody.sBiztype + '&nCashType=' + getBody.nCashType + "&sBiztypeList=" + getBody.sBiztypeList + "&nCashTypeList=" + getBody.nCashTypeList);
    window['$']('body').append(iframe);
    setTimeout("$('#downloadiframe').remove()", 60000);
  }
  // 点击表格头排序
  orderData(colName) {
    let listDataAsc = _.orderBy(this.dataList, [colName], ["asc"]);
    if (JSON.stringify(this.dataList) == JSON.stringify(listDataAsc)) {
      listDataAsc = _.orderBy(this.dataList, [colName], ["desc"]);
    }
    this.dataList = listDataAsc;
  }
}
